package com.hx.resource.controller;

import com.hx.common.core.controller.BaseController;
import com.hx.common.core.domain.AjaxResult;
import com.hx.common.utils.uuid.UUID;

import com.hx.resource.domain.FileInfo;
import com.hx.resource.service.IFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Map;

@RestController
public class FileUploadController extends BaseController {
    @Autowired
    private IFileInfoService fileInfoService;
    public static final long DEFAULT_MAX_SIZE = 500 * 1024 * 1024;

    @PostMapping("upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        System.out.println(file);
        Long userId = getUserId();//登录的用户ID
        String path1 = "E:\\5-1\\mall2\\mall-resource\\src\\main\\resources\\static\\img\\";
        String path2 = "E:\\5-1\\mall2\\mall-resource\\src\\main\\resources\\static\\video\\";
        String path3 = "E:\\5-1\\mall2\\mall-resource\\src\\main\\resources\\static\\doc\\";
        long size = file.getSize();
        String uuid = UUID.randomUUID().toString().replaceAll("_", "");
        String filename = file.getOriginalFilename();//上传原名
        String type = filename.substring(filename.lastIndexOf("."));

        InputStream inputStream = null;
        FileInfo fileInfo = new FileInfo();
        if (type.equals(".png") || type.equals(".jpg") || type.equals(".jpeg")) {
            file.transferTo(new File(path1 + uuid + type));
            inputStream = new FileInputStream(new File(path1 + uuid + type));
            fileInfo.setFileType(1L);
            fileInfo.setHttpPath("http://192.168.8.71:8890/img/" + uuid + type);
            fileInfo.setParentId(1L);
            fileInfo.setFilePath(path1);


        } else if (type.equals(".mp4") || type.equals(".wmv")) {
            file.transferTo(new File(path2 + uuid + type));
            inputStream = new FileInputStream(new File(path2 + uuid + type));
            fileInfo.setFileType(2L);
            fileInfo.setHttpPath("http://192.168.8.71:8890/video/" + uuid + type);
            fileInfo.setParentId(2L);
            fileInfo.setFilePath(path2);
        } else {
            file.transferTo(new File(path3 + uuid + type));
            inputStream = new FileInputStream(new File(path3 + uuid + type));
            fileInfo.setFileType(0L);
            fileInfo.setHttpPath("http://192.168.8.71:8890/doc/" + uuid + type);
            fileInfo.setParentId(0L);
            fileInfo.setFilePath(path3);
        }

        //获取文件MD5
        byte[] bytes = inputStream.readAllBytes();
        String md5 = DigestUtils.md5DigestAsHex(bytes);
        fileInfo.setFileSize(String.valueOf(size));
        fileInfo.setFileName(filename);

        fileInfo.setCrateDate(new Date());
        fileInfo.setUserId(userId);
        fileInfo.setState(1);

        fileInfoService.insertFileInfo(fileInfo);
        return AjaxResult.success("上传成功", Map.of("url", fileInfo.getHttpPath(), "name", filename));//返回的结果集
    }

}
